package com.hangwei.aicabinet.presenter;

import android.support.v4.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.model.bean.OrderBean;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.net.ViewCallBack;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.view.MainView;

import java.util.ArrayList;

import rx.Observable;

/**
 * Created by Dell on 2018/9/10.
 */

public class MainPresenter extends RecyclerViewPresenter<OrderBean, MainView> {

    public MainPresenter(MainView baseView, SwipeRefreshLayout refreshLayout, BaseQuickAdapter adapter, Object... params) {
        super(baseView, refreshLayout, adapter, params);
    }

    @Override
    protected Observable<ResponseInfo<ArrayList<OrderBean>>> getObservable(int page) {
        return NetManager.getResponseInfoAPI().getOrderEntryList(SPUtil.getString(SPKey.Account.PERSON_ID),
                page, 10, SPUtil.getString(SPKey.CABINET_ID),"0");
    }

    public void logout(final ViewCallBack viewCallBack) {
        if (viewCallBack != null)
            viewCallBack.onNetworkStart();
        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                return NetManager.getResponseInfoAPI().logout(SPUtil.getString(SPKey.CABINET_ID), "XIANPADLOGIN");
            }

            @Override
            protected void onNext(String data) {
                if (viewCallBack != null)
                    viewCallBack.onNetworkComplete(true);
            }

            @Override
            protected void onError() {
                if (viewCallBack != null)
                    viewCallBack.onNetworkComplete(false);
            }
        });
    }
}
